﻿using Hx.ADSyncPlatform.Db;
using Hx.ADSyncPlatform.Entity.Entities;
using Hx.ADSyncPlatform.Service.Base;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Hx.ADSyncPlatform.Service
{
    public class Plat_SysPermissionService : BaseService<Plat_SysPermission>
    {
        protected DbSet<Plat_SysRole> Plat_SysRoleDb;

        protected DbSet<Plat_SysRolePermission> Plat_SysRolePermissionDb;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public Plat_SysPermissionService(DbContext db) : base(db)
        {
            this.Plat_SysRoleDb = db.DbSet<Plat_SysRole>();
            this.Plat_SysRolePermissionDb = db.DbSet<Plat_SysRolePermission>();
        }



        /// <summary>
        /// 获取全部角色
        /// </summary>
        /// <returns></returns>
        public List<Plat_SysRole> GetRoles()
        {
            return Plat_SysRoleDb.GetList();
        }



        /// <summary>
        /// 根据用户id获取权限列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<List<Plat_SysPermission>> GetPermissionsByUserId(Guid userId)
        {

            var list = await base.dbClient.Queryable<Plat_SysManager, Plat_SysManagerRole, Plat_SysRole, Plat_SysRolePermission, Plat_SysPermission>
                           ((a, b, c, d, e) =>
                                new JoinQueryInfos(
                            JoinType.Inner, a.UserId == b.Plat_SysManager_ID,
                            JoinType.Inner, c.RoleID == b.Plat_SysRole_ID
                            , JoinType.Inner, c.RoleID == d.Role_Id,
                            JoinType.Inner, d.Permission_Id == e.PermissionId)
                            )
                           .Where((a, b, c, d, e) => a.UserId == userId)
                            .Select((a, b, c, d, e) => e).Distinct().ToListAsync();

            return list;
        }

        /// <summary>
        /// 根据角色获取权限列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<Plat_SysPermission>> GetPermissionsByRoleId(Guid roleId)
        {

            var list = await base.dbClient.Queryable<Plat_SysRole, Plat_SysRolePermission, Plat_SysPermission>
                           ((c, d, e) =>
                                new JoinQueryInfos(
                            JoinType.Inner, c.RoleID == d.Role_Id,
                            JoinType.Inner, d.Permission_Id == e.PermissionId)
                            )
                           .Where((c, d, e) => c.RoleID == roleId)
                            .Select((c, d, e) => e).ToListAsync();

            return list;
        }


        /// <summary>
        /// 保存角色权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="permissionIds"></param>
        /// <returns></returns>
        public async Task<bool> SaveRolePermissions(Guid roleId, List<Guid> permissionIds)
        {
            Plat_SysRolePermissionDb.Delete(n => n.Role_Id == roleId);
            List<Plat_SysRolePermission> sysRolePermissions = new List<Plat_SysRolePermission>();
            foreach (Guid permissionId in permissionIds)
            {
                Plat_SysRolePermission plat_SysRolePermission = new Plat_SysRolePermission()
                {
                    Permission_Id = permissionId,
                    Role_Id = roleId
                };
                sysRolePermissions.Add(plat_SysRolePermission);
            }
            return Plat_SysRolePermissionDb.InsertRange(sysRolePermissions);
        }


        /// <summary>
        /// 删除许可
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeletePermission(Guid id)
        {
            Plat_SysRolePermissionDb.Delete(n => n.Permission_Id == id);
            await base.Delete(n => n.PermissionId == id);
            return true;
        }


    }

}
